﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Checkout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CompareValidator1.ValueToCompare = DateTime.Now.ToString("M/d/yyyy");

        if (!IsPostBack)
        {
            this.Display();    
        }
    }
    void Display()
    {
        try
        {
            //Thong tin khach hang
            DataTable dtb = (DataTable)Session["Userlogin"];
            lblCustomerID.Text = dtb.Rows[0]["CustomerID"].ToString();
            lblFullName.Text = dtb.Rows[0]["FullName"].ToString();
            lblOrderDate.Text = DateTime.Now.ToString("M/d/yyyy HH:mm:ss");

            //Thong tin gio hang
            ShoppingCart cart = (ShoppingCart)Session["ShoppingCart"];
            GridView1.DataSource = cart;
            GridView1.DataBind();
            lblErrorDate.Visible = false;
        }
        catch
        {
            Response.Redirect("Default.aspx");
        }
    }
    protected void btnPlaceOrder_Click(object sender, EventArgs e)
    {       
            String sqlOrder = "INSERT INTO Orders(CustomerID, NgayDat, NgayNhan, NoiNhan, TrangThai) VALUES(@CustomerID, @NgayDat, @NgayNhan, @NoiNhan,N'Đặt hàng')";
            String sqlDetail = "INSERT INTO OrderDetail(OrderID, MaXe, GiaBan, SoLuongMua) VALUES(@OrderID, @MaXe, @GiaBan, @SoLuongMua)";
            String sqlSold = "UPDATE ThongTinCoBan SET DaBan=DaBan+@SoLuongMua WHERE MaXe=@MaXe";

            // INSERT VAO Orders
            try
            {
                DataBase.ExecuteNonQuery(sqlOrder,
                "@CustomerID", lblCustomerID.Text,
                "@NgayDat", DateTime.Now,
                "@NgayNhan", DateTime.Parse(txtRequiredDate.Text),
                "@NoiNhan", txtShipAddress.Text);
            }
            catch
            {
                lblErrorDate.Visible=true;
                return;
            }

            // Lay OrderID vua sinh ra
            String sqlOrderID = "SELECT MAX(OrderID) FROM Orders";
            int OrderID = (int)DataBase.ExecuteScalar(sqlOrderID);

            // INSERT vao OrderDetails va update so luong ban
            ShoppingCart cart = (ShoppingCart)Session["ShoppingCart"];
            foreach (DataRow Row in cart.Rows)
            {
                String maxe = Row["MaXe"].ToString();
                int soluong = (int)Row["SoLuongMua"];
                double gia = (double)Row["GiaBan"];
                DataBase.ExecuteNonQuery(sqlDetail,
                    "@OrderID", OrderID,
                    "@MaXe", maxe,
                    "@GiaBan", gia,
                    "@SoLuongMua", soluong);
                DataBase.ExecuteNonQuery(sqlSold, "@SoLuongMua", soluong, "@MaXe", maxe);
            }
            cart.Clear();
            Response.Redirect("ThankYou.aspx");           
                
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewCart.aspx");
    }
}
